<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Carbarn;
use App\Models\Order;
use App\Models\User;
use App\Models\WashJob;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use Encore\Admin\Widgets\Table;

class HomeController extends Controller
{
    public function index(Content $content)
    {
        return $content
            ->header('控制面板')
            ->description('运营监控')
            ->row(function (Row $row) {
                $row->column(3, function (Column $column) {
                    $column->append($this->infoBoxNewUser());
                });
                $row->column(3, function (Column $column) {
                    $column->append($this->infoBoxNewOrder());
                });
                $row->column(3, function (Column $column) {
                    $column->append($this->infoBoxNewIncome());
                });
                $row->column(3, function (Column $column) {
                    $column->append($this->infoBoxNewCarbarn());
                });
            })
            ->row(function (Row $row) {
                $bar = view('bar', [
                    'id' => 'order-trend-today',
                    'url' => route('order.trend-today'),
                    'type' => 'line',
                    'label' => '订单量',
                    'labelField' => 'hour',
                    'dataField' => 'counts',
                ]);
                $box = new Box('今日洗车订单增长趋势', $bar);
                $row->column(12, $box->style('info')->solid()->collapsable());

            })
            ->row(function (Row $row) {
                $bar = view('line', [
                    'id' => 'carbarn-top-today',
                    'url' => route('carbarn.top-today'),
                    'type' => 'horizontalBar',
                    'label' => '订单量',
                    'labelField' => 'name_formatted',
                    'dataField' => 'order_counts',
                ]);
                $box = new Box('洗车舱-今日排行', $bar);
                $row->column(1/2, $box->style('info')->solid()->collapsable());

                $bar = view('bar', [
                    'id' => 'station-top-today',
                    'url' => route('station.top-today'),
                    'type' => 'horizontalBar',
                    'label' => '订单量',
                    'labelField' => 'name',
                    'dataField' => 'order_counts',
                ]);
                $box = new Box('门店-今日排行', $bar);
                $row->column(1/2, $box->style('info')->solid()->collapsable());

            })
            ->row(function (Row $row) {

                $row->column(12, function (Column $column) {
                    $column->append($this->boxNewWashingUser());
                });

            });
    }

    private function infoBoxNewUser()
    {
        $info = User::where("regtime", ">=", strtotime('today'))->count();
        return new InfoBox('今日新增车主', 'users', 'aqua', route('user.index'), $info);
    }

    private function infoBoxNewOrder()
    {
        $info = Order::where("addTime", ">=", strtotime('today'))->count();
        return new InfoBox('今日订单', 'list-alt', 'aqua', route('order.index'), $info);
    }

    private function infoBoxNewIncome()
    {
        $info = Order::where("addTime", ">=", strtotime('today'))->sum('amount');
        return new InfoBox('今日收入', 'money', 'aqua', route('order.index'), $info);
    }

    private function infoBoxNewCarbarn()
    {
        $info = Carbarn::where("addTime", ">=", strtotime('today'))->count();
        return new InfoBox('今日新增洗车舱', 'car', 'aqua', route('carbarn.index'), $info);
    }

    private function boxNewWashingUser()
    {
        $jobs = WashJob::with(['user', 'carbarn'])->orderBy("id", "desc")->take(10)->get();
        $data = [];
        $jobs->each(function ($item) use (&$data) {
            $data[] = [
                sprintf('<img src="%s" class="img-circle img-sm" />', optional($item->user)->avatar),
                optional($item->user)->nickName,
                optional($item->carbarn)->address,
                $item->addTime ? date('Y-m-d H:i:s', $item->addTime) : "",
            ];
        });
        $headers = ['头像', '昵称', '洗车地址', '洗车时间'];
        $table = new Table($headers, $data);
        $box = new Box('正在洗车的车主', $table);
        return $box->solid()->style('primary');
    }

    public function test()
    {
        $c = collect([
            ['a' => 1, 'b' => ['c' => 444], 'ee' => 88, 'gg' => ['ff' => 88]],
        ]);
        $r = $c->pluck('a', 'ee')->get(88);
        dd($r);
    }
}
